package com.lk.web.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lk.web.entity.SysPermission;
import com.lk.web.mapper.SysPermissionMapper;
import com.lk.web.service.SysPermissionService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 权限表 服务实现类
 *
 * @author Aspirin
 * @since 2020-06-21
 */
@Service
public class SysPermissionServiceImpl extends ServiceImpl<SysPermissionMapper, SysPermission>
    implements SysPermissionService {

  @Override
  public List<SysPermission> findByUserId(Long userId) {
    if (userId == null) {
      return null;
    }
    List<SysPermission> permissionList = baseMapper.selectPermissionByUserId(userId);
    //   permissionList.remove(null); 如果没有权限，则将集合中的数据null移除
    return permissionList;
  }

  @Override
  public boolean deleteById(Long id) {
    // 1. 删除当前id的权限
    baseMapper.deleteById(id);
    // 2. 删除parent_id = id的权限，删除当前父权限的子权限
    LambdaQueryWrapper<SysPermission> queryWrapper = new LambdaQueryWrapper<>();
    //delete from sys_permission where parent_id = #{id};
    queryWrapper.eq(SysPermission::getParentId,id);
    baseMapper.delete(queryWrapper);
    return true;
  }

}
